﻿using AutoMapper.Internal.Mappers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Himp.Platform
{
    public class DataModelExtPropertyDictService : CrudAppService<DataModelExtPropertyDict, DataModelExtPropertyDictDto, Guid, DataModelExtPropertyDictInput, DataModelExtPropertyDictCreateDto, DataModelExtPropertyDictUpdateDto>
    {
        /// <summary>
        /// 
        /// </summary>
        private IRepository<DataModelExtPropertyDict, Guid> _stdModelAssocPhrDictRepository;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="StdModelConfigRepository"></param>
        public DataModelExtPropertyDictService(IRepository<DataModelExtPropertyDict, Guid> stdModelAssocPhrDictRepository) : base(stdModelAssocPhrDictRepository)
        {
            _stdModelAssocPhrDictRepository = stdModelAssocPhrDictRepository;
        }

        /// <summary>
        /// 获取标准单词
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public override async Task<PagedResultDto<DataModelExtPropertyDictDto>> GetListAsync(DataModelExtPropertyDictInput input)
        {
            var iQuery = await _stdModelAssocPhrDictRepository.WithDetailsAsync();
            iQuery = iQuery.Where(i => i.DataModelDictId == input.DataModelDictId);

            if (input.Pi == 0)
            {
                var itemsDto = ObjectMapper.Map<List<DataModelExtPropertyDict>, List<DataModelExtPropertyDictDto>>(iQuery.ToList());

                return new PagedResultDto<DataModelExtPropertyDictDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var totalCount = iQuery.Count();
                var items = iQuery.Page(input.Pi, input.Ps);
                var itemsDto = ObjectMapper.Map<List<DataModelExtPropertyDict>, List<DataModelExtPropertyDictDto>>(items.ToList());

                return new PagedResultDto<DataModelExtPropertyDictDto>(totalCount, itemsDto);
            }
        }
    }
}
